package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.PurseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.DialogUtils;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class WalletActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.image_header)
    CircleImageView imageHeader;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_order_income)
    TextView tvOrderIncome;
    @BindView(R.id.tv_tuiguang_income)
    TextView tvTuiguangIncome;
    @BindView(R.id.rl_withdraw)
    RelativeLayout rlWithdraw;
    @BindView(R.id.rl_psw)
    RelativeLayout rlPsw;
    private int isPsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getWalletInfo();
    }

    private void initView() {
        String headUrl= (String) SPUtils.get(this,Constant.HEAD_URL,"");
        Glide.with(this).load(headUrl).into(imageHeader);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleRightOneTv.setVisibility(View.VISIBLE);
        titleRightOneTv.setText("收支记录");
        tvMiddleTitle.setText("我的钱包");
    }
    private void getWalletInfo() {
        loadingDialog.show();
        OkgoUtil.post(Http.MyPurse,WalletActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        PurseBean parentBean= DealResult.getInstance().dealDatas(WalletActivity.this,response,new TypeToken<PurseBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            setDatas(parentBean.getBody());
                        }
//                        ToastUtils.toastForShort(WalletActivity.this,parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(WalletActivity.this,getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(PurseBean.BodyBean bean) {
        tvMoney.setText(bean.getAccountTotal());
        tvOrderIncome.setText(bean.getWagesTotal()+getString(R.string.order_income));
        tvTuiguangIncome.setText(bean.getExtendTotal()+getString(R.string.tuiguang_income));
        isPsw=bean.getIssetpwd();
        SPUtils.put(this,Constant.IS_BANK,bean.getIsyh());
        SPUtils.put(this,Constant.IS_ALIPAY,bean.getIsalipay());
        SPUtils.put(this,Constant.IS_WECHAT,bean.getIswx());
    }

    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv, R.id.rl_withdraw, R.id.rl_psw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_tv:
                startActivity(new Intent(this, IncomeHistoryActivity.class));
                break;
            case R.id.rl_withdraw:
                startActivity(new Intent(this, WithDrawActivity.class));
                break;
            case R.id.rl_psw:
//                是否设置密码（1是2否）
                if (isPsw == 1) {
                    final NormalDialog dialog = DialogUtils.getDialogMiddle(this, "", "已经设置过支付密码，是否重新设置");
                    dialog.setOnBtnClickL(new OnBtnClickL() {
                                              @Override
                                              public void onBtnClick() {
                                                  dialog.dismiss();
                                              }
                                          }, new OnBtnClickL() {
                                              @Override
                                              public void onBtnClick() {
                                                  getYzm();
                                                  dialog.dismiss();
                                              }
                                          }
                    );
                } else {
                    getYzm();
//                    startActivity(new Intent(this, SetPayPswActivity.class));
                }
                break;
        }
    }

    private void getYzm() {
        String phone= (String) SPUtils.get(this,Constant.PHONE,"");
        OkgoUtil.postNoUser(Http.YZM,this)
                .params("phone_no",phone)
                .params("status",5)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if(response==null){return;}
                        String res=response.body().toString();
                        BaseBean bean= new Gson().fromJson(res, BaseBean.class);
                        if(bean==null){return;};
                        if(bean.getCode()==Constant.SUCESS){
                            Intent i=new Intent(WalletActivity.this,SetPayPswActivity.class);
                            i.putExtra("flag",1);
                            startActivity(i);
                        }
                        ToastUtils.toastForShort(WalletActivity.this,bean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(WalletActivity.this,getString(R.string.bad_net));
                    }
                });
    }
}
